package com.hupu.jrs.ui.login

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.support.design.widget.TextInputLayout
import android.support.v7.widget.Toolbar
import android.text.Editable
import android.text.TextWatcher
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import com.afollestad.materialdialogs.MaterialDialog
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseSwipeBackActivity

/**
 * Created by sll on 2016/3/10.
 */
class LoginActivity : BaseSwipeBackActivity(), LoginContract.View {

    @Inject
    internal var mPresenter: LoginPresenter? = null

    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null
    @BindView(R.id.etUserName)
    internal var etUserName: EditText? = null
    @BindView(R.id.textInputUserName)
    internal var textInputUserName: TextInputLayout? = null
    @BindView(R.id.etPassWord)
    internal var etPassWord: EditText? = null
    @BindView(R.id.textInputPassword)
    internal var textInputPassword: TextInputLayout? = null

    private var dialog: MaterialDialog? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = true

    override fun initContentView(): Int {
        return R.layout.activity_login
    }

    override fun initInjector() {
        DaggerLoginComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .build()
                .inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        initToolBar(toolbar)
        setTitle("登录")
        dialog = MaterialDialog.Builder(this).title("提示").content("登录中").progress(true, 0).build()
        etUserName!!.addTextChangedListener(MTextWatcher(this!!.textInputUserName!!))
        etPassWord!!.addTextChangedListener(MTextWatcher(this!!.textInputPassword!!))
    }

    override fun showLoading() {
        if (!isFinishing() && !dialog!!.isShowing) {
            dialog!!.show()
        }
    }

    override fun hideLoading() {
        if (!isFinishing() && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }

    override fun showUserNameError(error: String) {
        textInputUserName!!.error = error
        textInputUserName!!.isErrorEnabled = true
    }

    override fun showPassWordError(error: String) {
        textInputPassword!!.error = error
        textInputPassword!!.isErrorEnabled = true
    }

    override fun loginSuccess() {
        Handler().postDelayed({ finish() }, 1500)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        getMenuInflater().inflate(R.menu.menu_login, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.login) {
            doLogin()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    private fun doLogin() {
        val mUserName = etUserName!!.text.toString().trim { it <= ' ' }
        val mPassword = etPassWord!!.text.toString().trim { it <= ' ' }
        mPresenter!!.login(mUserName, mPassword)
    }

    internal inner class MTextWatcher(var textInputLayout: TextInputLayout) : TextWatcher {

        override fun afterTextChanged(arg0: Editable) {

        }

        override fun beforeTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {

        }

        override fun onTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {
            textInputLayout.isErrorEnabled = false
        }
    }

    companion object {

        fun startActivity(mContext: Context) {
            val intent = Intent(mContext, LoginActivity::class.java)
            mContext.startActivity(intent)
        }
    }
}
